﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using YnPlcDataServer.Entities;
using YnPlcDataServer.Variable;

namespace YnPlcDataServer
{
    public partial class HistoryDataChart2Form : Form
    {
        #region 自定义变量
        public List<YnSvid> listYnSvid { get; set; }
        public List<YnSvData_hor> listYnSvData { get; set; }
        #endregion

        #region 窗体
        public HistoryDataChart2Form()
        {
            InitializeComponent();
        }
        private void HistoryDataChart2Form_Load(object sender, EventArgs e)
        {
            comboBox_daq_svid.DataSource = listYnSvid;
            comboBox_daq_svid.DisplayMember = "name";
            comboBox_daq_svid.ValueMember = "svid_id";
        }
        private void HistoryDataChart2Form_Shown(object sender, EventArgs e)
        {

        }
        private void button_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        #region 图形
        private void button_ok_Click(object sender, EventArgs e)
        {
            InitChart();
            try
            {
                AddChart();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示...", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void InitChart()
        {


        }
        private void AddChart()
        {
            try
            {
                YnSvid ynSvid_x = (YnSvid)comboBox_daq_svid.SelectedItem;
                if (ynSvid_x == null)
                {
                    MessageBox.Show("请选择X轴变量", "提示...", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                int svid_x=0;
                for (; svid_x < listYnSvid.Count; svid_x++)
                {
                    if (listYnSvid[svid_x].svid_id == ynSvid_x.svid_id)
                        break;
                }
                foreach (Control _control in this.panel_chart.Controls)
                {
                    if (_control is Chart)
                    {
                        this.panel_chart.Controls.Remove(_control);
                    }
                }

                Chart chart1 = new Chart();
                //chart1.Tag = ynSvid;
                ChartArea chartArea1 = new ChartArea();
                Legend legend1 = new Legend();
                Title title1 = new Title();

                //chartArea1.AxisX.LabelStyle.Format = "HH:mm";//:ss
                //设置间隔类型           
                //chartArea1.AxisX.IntervalType = System.Windows.Forms.DataVisualization.Charting..DateTimeIntervalType.Minutes;
                chartArea1.AxisX.Interval = 5;
                //chartArea1.AxisX.MajorGrid.Interval = 0D;
                //chartArea1.AxisX.Maximum = 43636.684328703705D;
                //chartArea1.AxisX.Minimum = 43636.64266203704D;
                chartArea1.AxisX.Title = ynSvid_x.name;

                //chartArea1.AxisY.Interval = 10D;
                //chartArea1.AxisY.MajorGrid.Interval = 30D;
                //chartArea1.AxisY.Maximum = 100D;
                chartArea1.AxisY.Minimum = 0D;
                chartArea1.AxisY.Title = "值";
                //chartArea1.Name = "ChartArea1";
                chart1.ChartAreas.Add(chartArea1);

                foreach(YnSvid ynSvid in listYnSvid)
                {
                    if (ynSvid.svid_id == ynSvid_x.svid_id)
                        continue;
                    Series seriesValue = new Series();
                    seriesValue.Tag = ynSvid;
                    //seriesValue.ChartArea = "ChartArea1";
                    seriesValue.ChartType = SeriesChartType.FastLine;
                    //seriesValue.XValueType = ChartValueType.Time;
                    seriesValue.XValueType = ChartValueType.Auto;
                    //seriesValue.Legend = "Legend1";
                    //seriesValue.Name = "Series1";
                    seriesValue.LegendText = ynSvid.name;
                    chart1.Series.Add(seriesValue);
                }

                chart1.Legends.Add(legend1);
                //chart1.Location = new System.Drawing.Point(0, 0);
                //chart1.Name = "chart2";
                chart1.Size = new System.Drawing.Size(930, 420);
                chart1.TabIndex = 0;
                //chart1.Text = "chart2";

                title1.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F);
                //title1.Name = "Title_Main";
                title1.Text = "历史数据趋势图";
                chart1.Titles.Add(title1);

                this.panel_chart.Controls.Add(chart1);
                chart1.BringToFront();
                chart1.Dock = System.Windows.Forms.DockStyle.Top;

                List<YnSvData_hor> listYnSvData_filter = new List<YnSvData_hor>();
                foreach (YnSvData_hor ynSvData_hor in listYnSvData)
                {
                    var property_x = ynSvData_hor.GetType().GetProperty("svid_id_" + svid_x);
                    object o_x = property_x.GetValue(ynSvData_hor);
                    decimal f_x = Convert.ToDecimal(o_x);
                    if (f_x > 0)
                        listYnSvData_filter.Add(ynSvData_hor);
                }

                if (listYnSvData_filter.Count > 0)
                {
                    //var property = YnSvData_hor.GetProperty("svid_id_");
                    //if (property != null)
                        //property.SetValue(ynSvData_hor, ynSvData.value, null);

                    YnSvData_hor YnSvData_start = listYnSvData_filter[0];
                    YnSvData_hor YnSvData_end = listYnSvData_filter[listYnSvData_filter.Count - 1];
                    var property_start = YnSvData_start.GetType().GetProperty("svid_id_" + svid_x);
                    object o_start = property_start.GetValue(YnSvData_start);
                    decimal f_start = Convert.ToDecimal(o_start);

                    var property_end = YnSvData_end.GetType().GetProperty("svid_id_" + svid_x);
                    object o_end = property_start.GetValue(YnSvData_end);
                    decimal f_end = Convert.ToDecimal(o_end);

                    if (f_start >= f_end)
                    {
                        throw new Exception("X轴变量必须是一个递增的变量");
                    }
                    chart1.ChartAreas[0].AxisX.Minimum = (int)f_start;
                    chart1.ChartAreas[0].AxisX.Maximum = (int)f_end;

                    foreach (YnSvData_hor ynSvData_hor in listYnSvData_filter)
                    {
                        //增加点
                        //seriesValue.Points.AddXY(ynSvData.createTime.ToOADate(), ynSvData.value);
                        var property_x = ynSvData_hor.GetType().GetProperty("svid_id_" + svid_x);
                        object o_x = property_x.GetValue(ynSvData_hor);
                        decimal f_x = Convert.ToDecimal(o_x);

                        int iSeries = 0;
                        for (int iSvid = 0; iSvid < listYnSvid.Count; iSvid++)
                        {
                            YnSvid ynSvid = listYnSvid[iSvid];
                            if (ynSvid.svid_id == ynSvid_x.svid_id)
                                continue;

                            var property_y = ynSvData_hor.GetType().GetProperty("svid_id_" + iSvid);
                            object o_y = property_y.GetValue(ynSvData_hor);
                            decimal f_y = Convert.ToDecimal(o_x);

                            chart1.Series[iSeries].Points.AddXY((int)f_x, f_y);
                            iSeries++;
                        }
                    }
                }

                /*
                List<YnSvData> _listYnSvData = listYnSvData.Where(item => item.svid_id == ynSvid.svid_id).ToList();

                if (_listYnSvData.Count > 0)
                {
                    DateTime dtStart = _listYnSvData[0].createTime;
                    DateTime dtEnd = _listYnSvData[_listYnSvData.Count - 1].createTime;
                    double dEnd = dtEnd.ToOADate();
                    double dStart = dtStart.ToOADate();
                    chart1.ChartAreas[0].AxisX.Minimum = dStart;
                    chart1.ChartAreas[0].AxisX.Maximum = dEnd;

                    foreach (YnSvData ynSvData in _listYnSvData)
                    {
                        //增加点
                        seriesValue.Points.AddXY(ynSvData.createTime.ToOADate(), ynSvData.value);
                        seriesUpper.Points.AddXY(ynSvData.createTime.ToOADate(), ynSvid.upper_spec_limit);
                        seriesLower.Points.AddXY(ynSvData.createTime.ToOADate(), ynSvid.lower_spec_limit);
                    }

                }*/
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误提示...", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        #endregion

    }
}
